package com.whales.lms.rms.web.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import anyframe.common.Page;

import com.whales.lms.rms.scheduler.service.IFService;
import com.whales.lms.rms.web.model.PcItemVO;
import com.whales.lms.rms.web.model.PcVO;
import com.whales.shared.exception.BaseException;
import com.whales.shared.service.GenericService;
import com.whales.shared.util.NetworkUtil;

@Service
public class PcService extends GenericService<PcVO, PcVO> {
	@Inject
	private PcItemService pcItemService;
	@Inject
	private IFService ifService;
	
	@Override
	public int create(PcVO vo) throws Exception {
		if(this.getList("pc.findByName",vo).size() > 0){
			throw new BaseException(super.getMessage("rms.msg.exitsDuplicatedSystem"));
		}
		vo.setId(super.getNewId());
		if(vo.getItemVOs() != null){
			for (PcItemVO itemVO  : vo.getItemVOs()) {
				itemVO.setParentId(vo.getId());
			}
		}
		pcItemService.update(vo.getItemVOs());
		//WOL을 위한 INI 파일 등록
		NetworkUtil.registWakeOnConfigFile(vo.getMacAddress(), vo.getIp());
		return super.create("pc.createPc", vo);
	}
	
	@Override
	public int delete(PcVO vo) throws Exception {
		for (int i = 0; i < vo.getIds().length; i++) {
			PcVO pcVO = new PcVO();			
			pcVO.setId(vo.getIds()[i]);			
			pcVO = this.findInfo(pcVO);
			//WOL을 위한 INI 파일 수정
			NetworkUtil.deleteWakeOnConfigFile(pcVO.getIp());
			//PC Master 삭제
			super.delete("pc.deletePcById",pcVO);
			//HW,SW 링크정보 삭제
			PcItemVO pcItemVO = new PcItemVO();
			pcItemVO.setParentId(pcVO.getId());
			pcItemService.delete(pcItemVO);
		}
		return super.delete(vo);
	}
	
	public PcVO findByIp(String ip) throws Exception{
		PcVO pcVO = new PcVO();
		pcVO.setIp(ip);
		return (PcVO)commonDao.findInfo("pc.getPcByIp", pcVO);
	}
	
	@Override
	public PcVO findInfo(PcVO vo) throws Exception {
		return super.findInfo("pc.getPcById",vo);
	}
	
	@Override
	public List<PcVO> getList(PcVO vo) throws Exception {
		return super.getList("pc.getPcList",vo);
	}
	
	@Override
	public Page getPageList(PcVO vo) throws Exception {
		return super.getPageList("pc.getPcList", vo, vo.getStart(), vo.getLimit());
	}
	
	public List<PcVO> getPcBySoftwareId(String softwareId) throws Exception{
		PcVO softwareVO = new PcVO();
		softwareVO.setId(softwareId);
		return super.getList("pc.getPcBySoftwareId", softwareVO);
	}
	
	@Override
	public int update(PcVO vo) throws Exception {
		PcItemVO deleteVO = new PcItemVO(); 
		deleteVO.setParentId(vo.getId());
		pcItemService.delete(deleteVO);
		pcItemService.update(vo.getItemVOs());
		//Agent에 Software update command 호출
		ifService.softwareVersionUpdate(vo.getIp());
		//WOL을 위한 INI 파일 수정
		NetworkUtil.registWakeOnConfigFile(vo.getMacAddress(), vo.getIp());
		return super.update("pc.updateById", vo);
	}
	
	public int updateStateByIp(PcVO vo) throws Exception{
		return super.update("pc.updateStateByIp", vo);
	}
	
	public int updateLifeTime(PcVO vo) throws Exception{
		return super.update("pc.updateLifeTimeByIp",vo);
	}
	
	public int updateStateById(PcVO vo) throws Exception{
		return super.update("pc.updateStateById",vo);
	}
}
